home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1 / Ian and Stuart's One (Australia).iso / Australasian Legends / Commercial / Rainbow Hill / MacDOS™ 2.0.0 / batches / search.bat < prev    next >
DOS Batch File  |  1993-10-30  |  3KB  |  109 lines

  1. @echo off
  2. !  search.bat    Batch file to search files for a string.
  3. !                The search is case insensitive and only the first occurrence
  4. !                of each file is found.
  5. !                Only files of type TEXT are scanned and the maximum line length
  6. !                is approximately 100 characters.
  7. !                Note that search.bat leaves CONFIRM ON
  8. !
  9. !  search name what [opt...]
  10. !
  11. !  where:
  12. !    name   = what is to be searched. It is either a folder name, or a file name
  13. !             (possibly wildcarded)
  14. !    what   = the string to be found
  15. !    opt... = options as specified for DIR, one option per argument, up to 6
  16. !
  17. !  Copyright © 1993 by Rainbow Hill Pty Ltd. All rights reserved.
  18. !
  19.  
  20.     ! initialisation
  21.     set search_file=TEMP8888
  22.     onerror ERR_LBL
  23.  
  24.     ! ensure that the first and second parameters are there
  25.     set doserr=13
  26.     if "%1 " == " " goto ERR_LBL
  27.     if "%2 " == " " goto ERR_LBL
  28.  
  29.     ! prepare a file with all filenames to be searched
  30.     dir %1 %3 %4 %5 %6 %7 %8 %9 /B/A-D/T=TEXT > %search_file%
  31.  
  32.     ! convert the string to uppercase so that the search is case insensitive
  33.     set search_string=%2
  34.     toupper search_string
  35.  
  36.     ! open the file containing the list of files to be searched
  37.     open %search_file% search_dirFileID
  38.  
  39.     ! scan the list of files
  40.     repeat DIR_LBL
  41.  
  42.         ! obtain a file name
  43.         onerror ENDDIR_LBL
  44.         read %search_dirFileID% search_fileName
  45.         ! remove the double quotes which enclose the names
  46.         decr search_fileName
  47.         decr -search_fileName
  48.  
  49.         ! open the file
  50.         onerror ERR_LBL
  51.         open %search_fileName% search_fileID
  52.         echo "%search_fileName%"
  53.  
  54.         ! reset the line count
  55.         set search_lineNum=0
  56.  
  57.         ! read the file
  58.         :FILE_LBL
  59.  
  60.             ! read one line
  61.             onerror ENDFILE_LBL
  62.             read %search_fileID% search_line
  63.             incr search_lineNum
  64.  
  65.             ! search the line for the string
  66.             set search_upLine=%search_line%
  67.             toupper search_upLine
  68.             onerror FILE_LBL
  69.             sstr search_upLine %search_string%
  70.             ! if we arrive here the string was found
  71.             echo   %search_lineNum%: %search_line%
  72.             set doserr=0
  73.             goto ENDFILE_LBL
  74.  
  75.         goto FILE_LBL
  76.  
  77.     :ENDFILE_LBL
  78.         ! the file was automatically closed when the EOF was reached
  79.         if %doserr% == 3 goto DIR_LBL
  80.         if not %doserr% == 0 goto ERR_LBL
  81.             onerror
  82.             close %search_fileID%
  83.  
  84.     :DIR_LBL
  85.  
  86. :ENDDIR_LBL
  87.     if not %doserr% == 3 goto ERR_LBL
  88.     goto DONE_LBL
  89.  
  90. :ERR_LBL
  91.     show %doserr%
  92.  
  93. :DONE_LBL
  94.     ! just in case...
  95.     onerror
  96.     close %search_fileID%
  97.     close %search_dirFileID%
  98.     confirm off
  99.     del %search_file%
  100.     confirm on
  101.     set search_string=
  102.     set search_dirFileID=
  103.     set search_fileName=
  104.     set search_fileID=
  105.     set search_lineNum=
  106.     set search_line=
  107.     set search_upLine=
  108.     set search_file=
  109.